package com.boarsoft.boar.config.app.biz;

import java.util.Set;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.app.AppBiz;
import com.boarsoft.boar.app.biz.AppBaseBizImpl;
import com.boarsoft.boar.config.entity.AppConfigInfo;
import com.boarsoft.boar.entity.AppInfo;
import com.boarsoft.boar.entity.DirInfo;
import com.boarsoft.boar.entity.FileInfo;

@Component
public class AppConfigBizImpl extends AppBaseBizImpl implements AppBiz {

	@Override
	@Transactional(readOnly = true)
	public AppInfo get(String id) {
		AppConfigInfo o = dao.get(AppConfigInfo.class, id);
		//dao.evict(o);
		// o.setFiles(null);
		// o.setInsts(null);
		// o.setLibs(null);
		return o;
	}

	@Override
	@Transactional
	public String copy(String sourceId, String parentId, String newId) {
		AppInfo o = this.get(sourceId);
		AppInfo n = this.transfer(o, AppConfigInfo.class);
		n.setId(newId);// //应用的id来自于前一步骤的dir_info表id
		DirInfo pd = dirBiz.get(parentId);
		String projId = dirBiz.lookup(pd, DirInfo.TYPE_PROJ);
		n.setProjId(projId);
		dao.save(n);
		Set<FileInfo> oldFiles = o.getFiles();
		if (oldFiles != null && oldFiles.size() > 0) {
			for (FileInfo file : oldFiles) {
				fileBiz.copy(file.getId(), parentId, newId);
			}
		}
		appLibBiz.copy(sourceId, newId);
		// appFileBiz.copy(sourceId, newId);
		appInstBiz.copy(sourceId, newId);
		return newId;
	}
}